package com.wlps.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;

/**
 * <p>
 *
 * </p>
 *
 * @author ZWYZY
 * @since 2020/9/1
 */
@Aspect
@Component
//@Order(2)
@ControllerAdvice

public class WebExceptionAspect {
    @Pointcut(value = "execution(public * com.wlps.*.*.*(..)))")
    private void webExceptionCut(){
        logger.info("执行");
    }
    private static final Logger logger = LoggerFactory.getLogger(WebExceptionAspect.class);
    //切点在webpointCut()
    @AfterThrowing(pointcut = "webExceptionCut()", throwing = "e")
    //controller类抛出的异常在这边捕获
    public void handleThrowing(JoinPoint joinPoint, Exception e) {
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        //开始打log
        logger.info("异常:" + e.getMessage());
        logger.info("异常所在类：" + className);
        logger.info("异常所在方法：" + methodName);
        logger.info("异常中的参数：");
        logger.info(methodName);
        for (int i = 0; i < args.length; i++) {
            logger.info(args[i].toString());
        }
    }
}
